from django.shortcuts import render
from django.shortcuts import redirect
from django.utils import timezone
from question.models import Answer
from django.core import serializers
from django.http import HttpResponse, JsonResponse
from login.models import User
import json
from datetime import date


# Create your views here.
def index(request):
    return render(request, 'question/questionnaire1.html')


def questionnaire(request):
    # 用户没有登录不能进入
    if not request.session.get('is_login', None):
        return redirect("/login")

    user_name = request.session['user_name']
    user = User.objects.get(name=user_name)
    answer_set = Answer.objects.filter(user=user).order_by('-date')
    info = {}
    info["num"] = user.num
    info["name"] = user.name
    info["dept"] = user.dept
    info["party"] = user.party
    info["isfirst"] = True
    if answer_set.count() < 1:
        pass;
    else:
        last_answer = answer_set[0]
        # 获得当前日期
        today = date.today()
        # 获得数据库里的日期
        last_day = last_answer.date
        # 如果今天已经打过卡
        if today == last_day:
            info["isfirst"] = False

        info["1"] = str(last_answer.self_health)
        info["2"] = str(last_answer.family_health)
        info["3"] = str(last_answer.work_state)
        info["4"] = str(last_answer.work_evironment)
        info["5"] = str(last_answer.mental_health)
        info["6"] = last_answer.special_case

    return render(request, 'question/question.html', {'info': json.dumps(info)})


def waitForComplete(request):
    return render(request, 'question/waitForComplete.html')


def alreadyCompleted(request):
    return render(request, 'question/alreadyCompleted.html')


def answer(request):
    # 获得当前用户的用户名
    user_name = request.session['user_name']
    user = User.objects.get(name=user_name)
    answer_set = Answer.objects.filter(user=user).order_by('-date')
    today = date.today()
    info = {}
    info["isfirst"] = False;
    info["name"] = user.name
    info["num"] = user.num
    info["dept"] = user.dept
    info["party"] = user.party

    if request.method == 'POST':
        # 获得各个单选按钮的值
        self_health = request.POST.get('1')
        family_health = request.POST.get('2')
        work_state = request.POST.get('3')
        work_environment = request.POST.get('4')
        mental_health = request.POST.get('5')
        special_case = request.POST.get('6')
        info["1"] = self_health
        info["2"] = family_health
        info["3"] = work_state
        info["4"] = work_environment
        info["5"] = mental_health
        info["6"] = special_case

        # 如果没有，就是添加
        if answer_set.count() < 1:
            pass;
        # 否则就是修改，先把之前的数据删掉
        else:
            last_answer = answer_set[0]
            last_day = last_answer.date
            # 如果今天已经打过卡
            if last_day == today:
                last_answer.delete()

        answer = Answer()

        answer.date = today
        answer.user = user

        answer.self_health = int(self_health)
        answer.family_health = int(family_health)
        answer.work_state = int(work_state)
        answer.work_evironment = int(work_environment)
        answer.mental_health = int(mental_health)
        answer.special_case = special_case
        answer.save()

    return render(request, 'question/question.html', {'info': json.dumps(info)})


def ajax_fresh(request):
    if request.method == 'GET':
        # 获得选择的日期(从前端传过来的时间是字符串类型的)
        day_choosed = request.GET.get("date")
        year, month, day = day_choosed.split('-')
        date_choosed = date(int(year), int(month), int(day))
        user_name = request.session['user_name']
        user = User.objects.get(name=user_name)
        info = {}
        try:
            # 查询用户选择的日期的打卡情况
            answer = Answer.objects.filter(user=user).filter(date=date_choosed)[0]

            info["1"] = str(answer.self_health)
            info["2"] = str(answer.family_health)
            info["3"] = str(answer.work_state)
            info["4"] = str(answer.work_evironment)
            info["5"] = str(answer.mental_health)
            info["6"] = answer.special_case
            info['ret'] = True
        except:
            info['ret'] = False
            print("You don't answer the question at the date you choose!")

    # return HttpResponse(json.dumps(user_dict))
    return JsonResponse(info)
